<html>
<head>
<script type="text/javascript" src="../jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../channel.js"></script>
<script type="text/javascript">

function onServerMessage(data) {
	div = $('<div/>');
	div.html(data.msg);
	
	$('#text').append(div);
}

$(document).ready(function(){

<?php 
	include '../Channel.php';

	$channel = Channel::createChannel();

	echo "var channelId = " . $channel->getId();
?>	
	
	var channel = new Channel("../channelpool.php", channelId, {onServerMessage: onServerMessage});
	channel.pool();

	$('#chatform').submit(function() {

		var to = $('#to').val();
		var msg = $('#msg').val();

		$('#msg').val('');

		$.get("send.php", {to: to, msg: msg}, function(data){console.log("message sent.")}, "json");
		
		return false;
	});
});

</script>
</head>
<body>

<div id="text">
</div>

<form id="chatform">
<input type="text" name="to" id="to"/>
<br>
<textarea name="msg" id="msg" rows="5" cols="15"></textarea>
<br>
<input type="submit"/>
</form>



</body>
</html>